﻿using DualPlaneFR4Paster.Common.Services;
using DualPlaneFR4Paster.ViewModels.Dialogs;
using DualPlaneFR4Paster.ViewModels.Dialogs.Camera;
using DualPlaneFR4Paster.ViewModels.Dialogs.PartNum;
using DualPlaneFR4Paster.ViewModels.Home;
using DualPlaneFR4Paster.Views;
using DualPlaneFR4Paster.Views.Dialogs;
using DualPlaneFR4Paster.Views.Dialogs.Camera;
using DualPlaneFR4Paster.Views.Dialogs.PartNum;
using DualPlaneFR4Paster.Views.Home;
using Prism.Ioc;
using Prism.Unity;
using System;
using System.Threading;
using System.Windows;

namespace DualPlaneFR4Paster
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : PrismApplication
    {
        private static Mutex _mutex = null;
        protected override void OnStartup(StartupEventArgs e)
        {
            const string appName = "DualPlaneFR4Paster";
            bool createdNew;

            _mutex = new Mutex(true, appName, out createdNew);

            if (!createdNew)
            {
                MessageBox.Show("DualPlaneFR4Paster件已开启", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                //app is already running! Exiting the application  
                Environment.Exit(-1);
            }

            base.OnStartup(e);
        }
        protected override Window CreateShell()
        {
            return Container.Resolve<MainWindow>();
        }

        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
            containerRegistry.Register<IDialogHostService, DialogHostService>();

            containerRegistry.RegisterSingleton<IParamService, ParamService>("Param");
            containerRegistry.RegisterSingleton<IGTSCardService, GTSCardService>("Top_A4");
            containerRegistry.RegisterSingleton<IGTSCardService, GTSCardService>("Top_A8");
            containerRegistry.RegisterSingleton<IGTSCardService, GTSCardService>("Bottom_A8");
            containerRegistry.RegisterSingleton<ICameraService, CameraService>("Floor1Up");
            containerRegistry.RegisterSingleton<ICameraService, CameraService>("Floor1Down");
            containerRegistry.RegisterSingleton<ICameraService, CameraService>("Floor2Up");
            containerRegistry.RegisterSingleton<ICameraService, CameraService>("Floor2Down");
            containerRegistry.RegisterSingleton<IServoModbusService, ServoModbusService>("ServoModbus");
            containerRegistry.RegisterSingleton<IPLCModbusService, PLCModbusService>("PLCModbus");
            containerRegistry.RegisterSingleton<IStepMotorModbusService, StepMotorModbusService>("StepMotorModbus");

            containerRegistry.RegisterForNavigation<TopFloor, TopFloorViewModel>();
            containerRegistry.RegisterForNavigation<BottomFloor, BottomFloorViewModel>();

            containerRegistry.RegisterDialog<TopFloorAxisDialog, TopFloorAxisDialogViewModel>();
            containerRegistry.RegisterDialog<BottomFloorAxisDialog, BottomFloorAxisDialogViewModel>();
            containerRegistry.RegisterDialog<AboutDialog, AboutDialogViewModel>();
            containerRegistry.RegisterDialog<PasswordDialog, PasswordDialogViewModel>();
            containerRegistry.RegisterDialog<MessageBoxDialog, MessageBoxDialogViewModel>();
            containerRegistry.RegisterDialog<FeederDialog, FeederDialogViewModel>();
            containerRegistry.RegisterDialog<PastePointDialog, PastePointDialogViewModel>();
            containerRegistry.RegisterDialog<PartNumDialog, PartNumDialogViewModel>();
            containerRegistry.RegisterDialog<AddNewPartNumDialog, AddNewPartNumDialogViewModel>();
            containerRegistry.RegisterDialog<CameraFilmCalibDialog, CameraFilmCalibDialogViewModel>();
            containerRegistry.RegisterDialog<CameraProductCalibDialog, CameraProductCalibDialogViewModel>();
            containerRegistry.RegisterDialog<CameraFilmVisionCalcDialog, CameraFilmVisionCalcDialogViewModel>();
            containerRegistry.RegisterDialog<CameraProductVisionCalcDialog, CameraProductVisionCalcDialogViewModel>();
            containerRegistry.RegisterDialog<PLCDialog, PLCDialogViewModel>();
            containerRegistry.RegisterDialog<ParameterDialog, ParameterDialogViewModel>();
            containerRegistry.RegisterDialog<AlarmDialog, AlarmDialogViewModel>();
        }
    }
}
